﻿using Architecture.Api.Application.Command.RoleCommand;
using Architecture.Api.Dtos;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using Architecture.Service;
using Architecture.Service.Impl;
using Architecture.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using Architecture.Domain.Dto;
using Architecture.Domain.RBAC;

namespace Architecture.Api.Controllers
{
    /// <summary>
    /// 角色API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        protected readonly IRoleService roleService;

        protected readonly IMediator _mediatR;
        /// <summary>
        /// 角色构造函数
        /// </summary>
        /// <param name="roleService"></param>
        public RoleController(IRoleService roleService, IMediator mediatR)
        {
            this.roleService = roleService;
            _mediatR = mediatR;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(RoleCreateCommand model)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var data = HttpContext.User;
                //获取角色Id
                var role = HttpContext.User.FindFirst(System.Security.Claims.ClaimTypes.Role)?.Value;
                result.Data=await _mediatR.Send(model);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> Delete([FromQuery]RoleDeleteCommand id)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data= await _mediatR.Send(id);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageListDto<Role>>> Show([FromQuery]RoleShowCommand role)
        {
            ApiResult<PageListDto<Role>> result = new ApiResult<PageListDto<Role>>();
            try
            {
                result.Data = await _mediatR.Send(role);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Role>> Backfill([FromQuery]RoleBackfillCommand id)
        {
            ApiResult<Role> result = new ApiResult<Role>();
            try
            {
                result.Data = await _mediatR.Send(id);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> Update(RoleUpdateCommand model)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(model);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 用户角色关联添加
        /// </summary>
        /// <param name="related"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetRelatedAdd(RelatedAddCommand related)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(related);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 部门，人员反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<object>> SectorBackfill([FromQuery] SectorBackfillCommand id)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await _mediatR.Send(id);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色员工名称反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<object>> Employee([FromQuery]employeeCommand id)
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await _mediatR.Send(id);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 真删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteRelated(Related model)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await roleService.RelatedDelete(model);
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<object>> RoleGetAll()
        {
            ApiResult<object> result= new ApiResult<object>();
            try
            {
                result.Data = await roleService.RoleGetAll();
            }
            catch (Exception ex)
            {
                result.Code=-1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
